﻿using System;
using System.Web;
using System.Data;

namespace msBsc.WebUi
{
    public partial class wfOrderDetail : msBsc.Cls.BPCustom
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                BindControls();
                //this.SetContrlValue("txtId", GetFormParam("ID"));
                //ShowData(GetFormParam("ID"));
                ShowData(this.Request.QueryString.Get("ID"), this.Request.QueryString.Get("NO"));
            }
        }
        private void BindControls()
        {
            if (!IsPostBack)
            {
            }
        }

        public DataRow drOrder = null;
        public DataRow drShop = null;
        private string strOrderId = "";

        private void ShowData(string strId,string strNo)
        {
            if (string.IsNullOrEmpty(strId) && string.IsNullOrEmpty(strNo))
            {
                this.ToErrPage("当前要查看的订单未找到，请返回重新选择！");
                return;
            }
            using (msBsc.AppLogic.DataAlc.CBizShpOrderForm oCDbAccess = new msBsc.AppLogic.DataAlc.CBizShpOrderForm())
            {
                System.Data.DataSet ds ;
                if (!string.IsNullOrEmpty(strId))
                {
                    ds = oCDbAccess.SelectById(strId);
                }
                else
                {
                    ds = oCDbAccess.SelectByNos(strNo);
                }
                if (ds.Tables[0].Rows.Count <= 0)
                {
                    this.ToErrPage("当前要查看的订单未找到，请返回重新选择！");
                    return;
                }
                else
                {
                    if (msBsc.SvRef.UserInfo.OperType != "0")
                    {
                        if (ds.Tables[0].Rows[0]["USER_NME"].ToString().ToUpper() != msBsc.SvRef.UserInfo.Operator.ToUpper())
                        {
                            this.ToErrPage("非法的访问，只能查看自己购买的订单！");
                            return;
                        }
                    }

                    DataSet dsshop = oCDbAccess.DataObj.QueryBySQL(@"
    SELECT
    PROVINCE_ID
    ,CITY_ID
    ,AREA_ID
    ,ZONE_DESC
    ,CONTACTS
    ,CONTACTS_EMAIL
    ,CONTACTS_TEL
    ,CONTACTS_ADDRESS
    ,CONTACTS_PHONE FROM SHP_COMMERCIAL_TENANT_INFO WHERE COMM_TENANT_ID='" + ds.Tables[0].Rows[0]["COMM_TENANT_ID"].ToString() + "'");
                    drShop = dsshop.Tables[0].Rows[0];

                    strOrderId = ds.Tables[0].Rows[0]["ORDER_FORM_ID"].ToString();
                    this.ExecJScript("$('#txtId').val('" + strOrderId + "');");
                    drOrder = ds.Tables[0].Rows[0];
                }
            }
        }

        public string GetSteps()
        {
            System.Text.StringBuilder sbHtml = new System.Text.StringBuilder();
            if (drOrder["TRADE_STATUS_TXT"].ToString() == "等待买家付款")
            {
                sbHtml.Append(@"
<div class=""flow-steps-order "">
    <ol class=""num4"">
    <li class="" current ""><span class=""state"">拍下宝贝</span> <span class=""datetime"">" + drOrder["INITIAL_DT"].ToString() + @"</span> </li>
    <li class=""""><span class=""state"">付款到优速购商城</span> </li>
    <li class=""""><span class=""state"">卖家发货</span> </li>
    <li class=""last ""><span class=""state"">确认收货</span> </li>
    </ol>
</div>");
            }
            else if (drOrder["TRADE_STATUS_TXT"].ToString() == "买家已付款")
            {
                sbHtml.Append(@"
<div class=""flow-steps-order "">
    <ol class=""num4"">
    <li class="" done ""><span class=""state"">拍下宝贝</span> <span class=""datetime"">" + drOrder["INITIAL_DT"].ToString() + @"</span> </li>
    <li class="" current ""><span class=""state"">付款到优速购商城</span> <span class=""datetime"">" + drOrder["PAYMENT_DT"].ToString() + @"</span>  </li>
    <li class=""""><span class=""state"">卖家发货</span> </li>
    <li class=""last ""><span class=""state"">确认收货</span> </li>
    </ol>
</div>");
            }
            else if (drOrder["TRADE_STATUS_TXT"].ToString() == "卖家已发货")
            {
                sbHtml.Append(@"
<div class=""flow-steps-order "">
    <ol class=""num4"">
    <li class="" done ""><span class=""state"">拍下宝贝</span> <span class=""datetime"">" + drOrder["INITIAL_DT"].ToString() + @"</span> </li>
    <li class="" done ""><span class=""state"">付款到优速购商城</span> <span class=""datetime"">" + drOrder["PAYMENT_DT"].ToString() + @"</span>  </li>
    <li class="" current ""><span class=""state"">卖家发货</span> <span class=""datetime"">" + drOrder["DELIVER_DT"].ToString() + @" </li>
    <li class=""last ""><span class=""state"">确认收货</span> </li>
    </ol>
</div>");
            }
            else if (drOrder["TRADE_STATUS_TXT"].ToString() == "交易成功")
            {
                sbHtml.Append(@"
<div class=""flow-steps-order "">
    <ol class=""num4"">
    <li class="" done ""><span class=""state"">拍下宝贝</span> <span class=""datetime"">" + drOrder["INITIAL_DT"].ToString() + @"</span> </li>
    <li class="" done ""><span class=""state"">付款到优速购商城</span> <span class=""datetime"">" + drOrder["PAYMENT_DT"].ToString() + @"</span>  </li>
    <li class="" done ""><span class=""state"">卖家发货</span> <span class=""datetime"">" + drOrder["DELIVER_DT"].ToString() + @" </li>
    <li class=""last  current ""><span class=""state"">确认收货</span> <span class=""datetime"">" + drOrder["AFFIRM_DT"].ToString() + @" </li>
    </ol>
</div>");
            }

            return sbHtml.ToString();

        }

        public string GetOrderTracert()
        {
            System.Text.StringBuilder sbHtml = new System.Text.StringBuilder();
            if (drOrder["TRADE_STATUS_TXT"].ToString() == "等待买家付款")
            {
                sbHtml.Append(@"
<i class=""step-point step-point-1-4""></i>
<div class=""bd"">
    <div class=""trade-status"">
        <h3>
            当前订单状态： 宝贝已拍下，等待买家付款，请在3天内付款；若未及时付款，系统将自动取消订单
        </h3>
        <ol>
            <li>点击这里 <span class=""skin-orange""><a class=""button J_MakePoint"" href=""/c_PayCashier.htm?ID=" + drOrder["ORDER_FORM_NO"].ToString() + @""" target=""_self""
                title=""付款到优速购商城"">付款</a> </span></li>
            <li>如果您不想购买，可以 <a class=""J_DialogTrigger J_MakePoint"" href=""javascript:void(0);"" onclick=""doaction('docancelorder');"" target=""_self"">取消订单</a></li>
        </ol>
    </div>
      </div>");
            }
            else if (drOrder["TRADE_STATUS_TXT"].ToString() == "买家已付款")
            {
                sbHtml.Append(@"
<i class=""step-point step-point-2-4""></i>
<div class=""bd"">
    <div class=""trade-status"">
        <h3>
            当前订单状态： 买家已付款，卖家将在3天内发货
        </h3>
        <ol>
            <li>点击这里 <a class=""J_DialogTrigger J_MakePoint""   href=""javascript:void(0);"" onclick=""doaction('dotipsellerdeli');"" target=""_self"" style=""display:none;"">提醒卖家发货</a></li>
            <li>如果您不想购买，可以 <a class=""J_DialogTrigger J_MakePoint""  href=""javascript:void(0);"" onclick=""doaction('toreturncharge');"" target=""_self"">申请退款</a></li>
        </ol>
    </div>
      </div>");
            }
            else if (drOrder["TRADE_STATUS_TXT"].ToString() == "卖家已发货")
            {
                sbHtml.Append(@"
<i class=""step-point step-point-3-4""></i>
<div class=""bd"">
    <div class=""trade-status"">
        <h3>
            当前订单状态： 卖家已发货，请查看页面下方物流信息了解宝贝寄送情况
        </h3>
        <ol>
            <li>如果您已收到货，且对商品满意，您可以 <span class=""skin-blue""><a class=""long-button J_MakePoint""
                target=""_self"" href=""/c_ConfirmReceipt.htm?ID=" + drOrder["ORDER_FORM_ID"].ToString() + @""">确认收货</a> 打款给卖家 </span></li>
            <li>如果还未收到货，请注意自动打款时间，您可以 <a id=""buyerDelaybtn"" class=""J_MakePoint"" href=""javascript:void(0);"" onclick=""doaction('tolongreceipt');"" target=""_self"">
                延长收货时间</a> 或 申请退款 </li>
        </ol>
    </div>");
                using (msBsc.AppLogic.DataAlc.CBizShpOrderForm objorder = new AppLogic.DataAlc.CBizShpOrderForm())
                {
                    sbHtml.Append(@"
<div class=""logistics-list"">
    <h3>
        物流信息</h3>
    <hr>
    <dl>
        <dt>发货方式：</dt>
        <dd>
            快递
        </dd>
        <dt>物流公司：</dt>
        <dd class=""logistics-company"">
            " + drOrder["EXPRESS_CMP_NME"].ToString() + @"&nbsp;
        </dd>
        <dt>运单号码：</dt>
        <dd class=""logistics-id"">
            <span class=""log-id"">" + drOrder["WAYBILL_NUMBER"].ToString() + @"  </span>
        </dd>
        <dt id=""J_LogTrack"">物流跟踪：</dt>
        <dd id=""J_LogTrackText"">
            <ol id=""J_ExList"">
" + objorder.GetLogisticInfo(strOrderId) + @"
                <li class=""""></li>
            </ol>
        </dd>
    </dl>
    <div class=""clearfix"">
    </div>
</div>
      </div>");
                }
            }
            else if (drOrder["TRADE_STATUS_TXT"].ToString() == "交易成功")
            {
                sbHtml.Append(@"
<i class=""step-point step-point-4-4""></i>
<div class=""bd"">
    <div class=""trade-status"">
        <h3>
            当前订单状态： 交易已成功
        </h3>
        <ol>
            <li>如果没有收到货，或收到货后出现问题，您可以<span class=""ww-light ww-large"" data-display=""inline"">联系卖家</span>协商解决。</li>
            <li>如果卖家没有履行应尽的承诺，您可以“投诉维权”</li>
        </ol>
    </div>");
                using (msBsc.AppLogic.DataAlc.CBizShpOrderForm objorder = new AppLogic.DataAlc.CBizShpOrderForm())
                {
                    sbHtml.Append(@"
<div class=""logistics-list"">
    <h3>
        物流信息</h3>
    <hr>
    <dl>
        <dt>发货方式：</dt>
        <dd>
            快递
        </dd>
        <dt>物流公司：</dt>
        <dd class=""logistics-company"">
            圆通速递
        </dd>
        <dt>运单号码：</dt>
        <dd class=""logistics-id"">
            <span class=""log-id"">800082207581 </span>
        </dd>
        <dt id=""J_LogTrack"">物流跟踪：</dt>
        <dd id=""J_LogTrackText"">
            <ol id=""J_ExList"">
" + objorder.GetLogisticInfo(strOrderId) + @"
                <li class=""""></li>
            </ol>
        </dd>
    </dl>
    <div class=""clearfix"">
    </div>
</div>
      </div>");
                }
            }
            else if (drOrder["TRADE_STATUS_TXT"].ToString() == "交易关闭")
            {
                sbHtml.Append(@"
<div class=""bd"">
    <div class=""trade-status"">
        <h3>
            当前订单状态： 交易关闭
        </h3>
        <ul class=""expanded""> 
            <li>原因： "+drOrder["ORDER_CLOSE_DESC"].ToString()+@"</li>
        </ul>
    </div>
    </div>");
            }
            else if (drOrder["TRADE_STATUS_TXT"].ToString() == "退款中的订单")
            {
                sbHtml.Append(@"
<div class=""bd"">
    <div class=""trade-status"">
        <h3>
            当前订单状态： 正在申请退款
        </h3>
        <ul class=""expanded""> 
        </ul>
    </div>
    </div>");
            }

            return sbHtml.ToString();

        }

        public string GetNewMsg()
        {
            System.Text.StringBuilder sbHtml = new System.Text.StringBuilder();
            if (drOrder["TRADE_STATUS_TXT"].ToString() == "等待买家付款")
            {
                sbHtml.Append(@"
    <dt>补充留言：</dt>
    <dd class=""msg-input"">
        <textarea name=""msg"" class=""new-msg"" id=""J_NewMsgBox""></textarea>
        <div class=""msg add-msg"">
            <p class=""tips"">
                付款前，您可以对留言信息进行补充。</p>
        </div>
        <div class=""operate skin-blue"">
            <span class=""skin-orange""><a class=""button J_MakePoint""  href=""javascript:void(0);"" onclick=""doaction('addnewmsg');"" target=""_self"" title=""补充留言信息"" id=""J_DoAddMsg"">确定</a> </span>                                            
        </div>
    </dd>");
            //还可以输入<span id=""J_CurrMsgLength"">300</span>字<span class=""hidden"" id=""J_MaxMsgLength""></span>
            }
            return sbHtml.ToString();
        }

        public string GetAddrInfo()
        {
            return drOrder["TO_RECEIPT_NME"].ToString()
            + "," + drOrder["TO_PHONE_NO"].ToString()
            + " " + drOrder["TO_LANDLINE_NO"].ToString()
            + "," + drOrder["TO_ZONE_DESC"].ToString()
            + " " + drOrder["TO_STREET_ADDRESS"].ToString()
            + "," + drOrder["TO_ZIP_CODE"].ToString();
        }

        public string GetOrderListTableRow()
        {
            System.Text.StringBuilder sbHtml = new System.Text.StringBuilder();
            using (msBsc.AppLogic.DataAlc.CBizShpOrderFormList objlist = new AppLogic.DataAlc.CBizShpOrderFormList())
            {
                DataSet dslist = objlist.SelectByOrderId(strOrderId);
                for (int i = 0; i < dslist.Tables[0].Rows.Count; i++)
                {
                    string strDisInfo = "";
                    if (decimal.Parse(dslist.Tables[0].Rows[i]["DISCOUNT_AMT"].ToString() != string.Empty ? dslist.Tables[0].Rows[i]["DISCOUNT_AMT"].ToString() : "0.00") != 0M)
                    {
                        strDisInfo += "<li>卖家优惠" + dslist.Tables[0].Rows[i]["DISCOUNT_AMT"].ToString() + "元</li>";
                    }
                    string strAmtInfo="";
                    if(i==0)
                    {
                        strAmtInfo=@"
    <td class=""order-price"" rowspan="""+dslist.Tables[0].Rows.Count.ToString()+@""">
        " + drOrder["GOODS_AMT"].ToString() + @"
        <li>(快递 : " + drOrder["LOGISTICS_AMT"].ToString() + @" )</li>
    </td>";
                    }
                    sbHtml.Append(@"
    <tr class=""order-item"">
        <td class=""item"">
            <div class=""pic-info"">
                <div class=""pic s50"">
                    <a hidefocus=""true"" href=""/c_GoodsView.htm?ID=" + dslist.Tables[0].Rows[i]["GOODS_ID"].ToString() + @"""
                        target=""_blank"" title=""商品图片"">
                        <img alt=""查看宝贝详情"" src=""" + dslist.Tables[0].Rows[i]["IMG_URL"].ToString() + @""">
                    </a>
                </div>
            </div>
            <div class=""txt-info"">
                <div class=""desc"">
                    <span class=""name""><a href=""/c_GoodsView.htm?ID=" + dslist.Tables[0].Rows[i]["GOODS_ID"].ToString() + @"""
                        title=""" + dslist.Tables[0].Rows[i]["GOODS_NME"].ToString() + @""" target=""_blank"">" + dslist.Tables[0].Rows[i]["GOODS_NME"].ToString() + @"</a></span>
                    <br>
                </div>
            </div>
        </td>
        <td class=""sku"">
            " + dslist.Tables[0].Rows[i]["NORMS_DESC"].ToString() + @"
        </td>
        <td class=""status"">
            " + drOrder["TRADE_STATUS_TXT"].ToString() + @"
        </td>
        <td class=""service"">
            <p> </p>
        </td>
        <td class=""price"">
            " + dslist.Tables[0].Rows[i]["GOODS_STRIKE_PRICE"].ToString() + @"
        </td>
        <td class=""num"">
            " + dslist.Tables[0].Rows[i]["GOODS_QTY"].ToString() + @"
        </td>
        <td class=""discount"">
            <ul>
                " + strDisInfo + @"
            </ul>
        </td>
        " + strAmtInfo + @"
    </tr>");
                }
            }
             
            return sbHtml.ToString();
        }

        public override void DoAction()
        {
            if (this.strAction == "docancelorder")
            {
                System.Transactions.TransactionOptions transactionOption = new System.Transactions.TransactionOptions();
                transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
                transactionOption.Timeout = new TimeSpan(0, 10, 0);
                using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required, transactionOption))
                {
                    using (msBsc.AppLogic.DataAlc.CBizShpOrderForm objorder = new AppLogic.DataAlc.CBizShpOrderForm())
                    {
                        objorder.CancelOrder(this.GetParamHtValue("ID"));
                        this.ExecJScript("window.location.reload(true);");
                    }
                    ts.Complete();
                }
            }
            else if (this.strAction == "dotipsellerdeli")
            {
                System.Transactions.TransactionOptions transactionOption = new System.Transactions.TransactionOptions();
                transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
                transactionOption.Timeout = new TimeSpan(0, 10, 0);
                using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required, transactionOption))
                {
                    using (msBsc.AppLogic.DataAlc.CBizShpOrderForm objorder = new AppLogic.DataAlc.CBizShpOrderForm())
                    {
                        objorder.TipSellerDeli(this.GetParamHtValue("ID"));
                        this.ExecJScript("window.location.reload(true);");
                    }
                    ts.Complete();
                }
            }
            else if (this.strAction == "toreturncharge")
            {
                System.Transactions.TransactionOptions transactionOption = new System.Transactions.TransactionOptions();
                transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
                transactionOption.Timeout = new TimeSpan(0, 10, 0);
                using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required, transactionOption))
                {
                    using (msBsc.AppLogic.DataAlc.CBizShpOrderForm objorder = new AppLogic.DataAlc.CBizShpOrderForm())
                    {
                        objorder.ReturnCharge(this.GetParamHtValue("ID"));
                        this.ExecJScript("window.location.reload(true);");
                    }
                    ts.Complete();
                }
            }
            else if (this.strAction == "tolongreceipt")
            {
                System.Transactions.TransactionOptions transactionOption = new System.Transactions.TransactionOptions();
                transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
                transactionOption.Timeout = new TimeSpan(0, 10, 0);
                using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required, transactionOption))
                {
                    using (msBsc.AppLogic.DataAlc.CBizShpOrderForm objorder = new AppLogic.DataAlc.CBizShpOrderForm())
                    {
                        objorder.SetLongAutoReceiptDt(this.GetParamHtValue("ID"));
                        this.ExecJScript("window.location.reload(true);");
                    }
                    ts.Complete();
                }
            }
            else if (this.strAction == "addnewmsg")
            {
                System.Transactions.TransactionOptions transactionOption = new System.Transactions.TransactionOptions();
                transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
                transactionOption.Timeout = new TimeSpan(0, 10, 0);
                using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required, transactionOption))
                {
                    using (msBsc.AppLogic.DataAlc.CBizShpOrderForm objorder = new AppLogic.DataAlc.CBizShpOrderForm())
                    {
                        objorder.AddNewBuyerMsg(this.GetParamHtValue("ID"), this.GetParamHtValue("NEW_MSG"));
                        this.ExecJScript("window.location.reload(true);");
                    }
                    ts.Complete();
                }
            }
        }
    }
}